package com.back.baseModule.service.impl;


import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.back.baseModule.entity.SysUser;
import com.back.config.ConstConfig;
import com.back.util.DateUtil;
import com.back.baseModule.service.SysUserService;
import com.back.util.PassWordUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.back.util.UuidUtil;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.back.baseModule.dao.SysUserDao;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
	private SysUserDao sysUserDao;

    /**
	 * 系统管理员登录
	 * */
	@Override
	public SaTokenInfo login(SysUser sysUser) {
	    //登录 返回token
		SaTokenInfo saTokenInfo = null;
		SysUser adminUser = sysUserDao.findByCondition(sysUser);
		if (adminUser == null) {
			return null;
		}
		if("0".equals(adminUser.getStatus().toString())){
			return null;
		}
		if (!SaSecureUtil.md5(sysUser.getPassword()).equals(adminUser.getPassword())) {
			return null;
		}
		// 密码校验成功后登录，一行代码实现登录
		StpUtil.login(adminUser.getId());
		// 获取当前登录用户Token信息
		saTokenInfo = StpUtil.getTokenInfo();
		return saTokenInfo;
	}

	/**系统管理员表列表
	 */
    @Override
    public List<Map<String, Object>> getList(Map<String, Object> map){
       if( map.get("pageNum") != null && map.get("pageSize") != null &&
				!map.get("pageNum").toString().isEmpty() && !map.get("pageSize").toString().isEmpty()){
			PageHelper.startPage(Integer.parseInt(map.get("pageNum").toString()) , Integer.parseInt(map.get("pageSize").toString()));
		}
        return sysUserDao.getList(map);
    }

	/**
	 * 根据条件查找系统管理员表信息
	 * */
    @Override
    public SysUser findByCondition(SysUser sysUser){
       return sysUserDao.findByCondition(sysUser);
    }
    /**
	 * 新增系统管理员表
	 */
    public Map<String, Object> save(SysUser sysUser){
		Map<String, Object> map = new HashMap<>();
		if(sysUser == null
				|| sysUser.getName() == null || "".equals(sysUser.getName())
				|| sysUser.getTel() == null || "".equals(sysUser.getTel())){
			map.put("rCode", ConstConfig.FAIL_CODE_ML);
			map.put("rMsg", ConstConfig.FAIL_MSG_IN_QCS);
		}else{
			SysUser user1 = new SysUser();
			user1.setTel(sysUser.getTel());
			SysUser user2 = sysUserDao.findByCondition(user1);
			//查询是否存在  没有新插入  有的
			if(user2 == null){
				sysUser.setId(UuidUtil.get32UUID());
				sysUser.setCreateTime(new Date());
				sysUser.setSex(true);
				sysUser.setPassword(PassWordUtil.getPassWord(sysUser.getTel()));

				int types = sysUserDao.save(sysUser);
				if(types > 0){
					map.put("rCode", ConstConfig.SUCCESS_CODE_ML);
					map.put("rMsg", "添加成功");
				}else{
					map.put("rCode", ConstConfig.FAIL_CODE_ERROR);
					map.put("rMsg", ConstConfig.FAIL_MSG_IN_ERROR);
				}
			}else{
				map.put("rCode", ConstConfig.FAIL_CODE_ERROR);
				map.put("rMsg", "已存在相同电话号码的用户");
			}
		}
		return map;
	}
	/**
	 * 修改系统管理员表
	 */
    public Map<String, Object> updateById(SysUser sysUser){
		Map<String, Object> map = new HashMap<>();
		if(sysUser == null || sysUser.getId() == null || "".equals(sysUser.getId())){
			map.put("rCode", ConstConfig.FAIL_CODE_ML);
			map.put("rMsg", ConstConfig.FAIL_MSG_IN_QCS);
		}else{
			//密码重置
			String tel = sysUser.getTel();
			if (null != tel && !"".equals(tel)) {
				sysUser.setPassword(PassWordUtil.getPassWord(tel));
			}
			int types = sysUserDao.updateById(sysUser);
			if(types > 0){
				map.put("rCode", ConstConfig.SUCCESS_CODE_ML);
				map.put("rMsg", ConstConfig.SUCCESS_MSG_UP);
			}else{
				map.put("rCode", ConstConfig.FAIL_CODE_ERROR);
				map.put("rMsg", ConstConfig.FAIL_MSG_IN_ERROR);
			}
		}
		return map;

	}
	/**
	 * 删除系统管理员表
	 */
    public Map<String, Object> delete(SysUser sysUser){
		Map<String, Object> map = new HashMap<>();
		if(sysUser == null || sysUser.getId() == null || "".equals(sysUser.getId())){
			map.put("rCode", ConstConfig.FAIL_CODE_ML);
			map.put("rMsg", ConstConfig.FAIL_MSG_IN_QCS);
		}else{
			int types = sysUserDao.delete(sysUser);
			if(types > 0){
				map.put("rCode", ConstConfig.SUCCESS_CODE_ML);
				map.put("rMsg", ConstConfig.SUCCESS_MSG_DEL);
			}else{
				map.put("rCode", ConstConfig.FAIL_CODE_ERROR);
				map.put("rMsg", ConstConfig.FAIL_MSG_IN_ERROR);
			}
		}
		return map;
	}

}

